﻿using vJoyInterfaceWrap;

namespace QAQ
{
    class JoyControl
    {
        // Declaring one joystick (Device id 1) and a position structure. 
        public vJoy joystick;
        private vJoy.JoystickState iReport;
        public uint id = 2;
        public string sta;
        public const int KEY_SPACE = 0x1 << 16;
        public const int KEY_UP = 0x1 << 17;
        public const int KEY_DOWN = 0x1 << 18;
        public const int KEY_LEFT = 0x1 << 19;
        public const int KEY_RIGHT = 0x1 << 20;
        public const int KEY_MOUSELEFT = 0x1 << 21;
        public const int KEY_FARM = 0x1 << 22;
        public const int KEY_F9 = 0x1 << 23;
        public const int KEY_ENTER = 0x1 << 24;
        public const int KEY_ESC = 0x1 << 25;
        public const int KEY_PASTE= 0x1 << 26;
        
        
            
        //initializer
        public JoyControl()
        {
            joystick = new vJoy();         
           
            iReport = new vJoy.JoystickState();
            if (!joystick.vJoyEnabled())
            {
                sta="vJoy driver not enabled: Failed Getting vJoy attributes.";
                return;
            }
            else
                sta= "Vendor: " + joystick.GetvJoyManufacturerString() + "\nProduct :" + joystick.GetvJoyProductString() + "\nVersion Number:" + joystick.GetvJoySerialNumberString();
            
            iReport.AxisXRot = 16500;
            iReport.AxisYRot = 16500;
            iReport.AxisZRot = 16500;
            iReport.AxisX = 16500;
            iReport.AxisY = 16500;
            iReport.AxisZ = 16500;
            if (!joystick.UpdateVJD(id, ref iReport))
            {
                joystick.AcquireVJD(id);
            }

        }


        public void Key(int k)
        {
            iReport.bDevice = (byte)id;
            iReport.Buttons |= (uint)(k);

            /// Feed the driver with the position packet - is fails then wait for input then try to re-acquire device
            if (!joystick.UpdateVJD(id, ref iReport))
            {
                joystick.AcquireVJD(id);
            }

            System.Threading.Thread.Sleep(33);

            iReport.Buttons &= ~(uint)(k);
            if (!joystick.UpdateVJD(id, ref iReport))
            {
                joystick.AcquireVJD(id);
            }

        }

        public void KeyUp(int k)
        {
            iReport.bDevice = (byte)id;
            iReport.Buttons &= ~((uint)(k));

            /// Feed the driver with the position packet - is fails then wait for input then try to re-acquire device
            if (!joystick.UpdateVJD(id, ref iReport))
            {
                joystick.AcquireVJD(id);
            }
        }

        public void KeyDown(int k)
        {
            iReport.bDevice = (byte)id;
            iReport.Buttons |= (uint)(k);

            /// Feed the driver with the position packet - is fails then wait for input then try to re-acquire device
            if (!joystick.UpdateVJD(id, ref iReport))
            {
                joystick.AcquireVJD(id);
            }
        }

    }
}
